#include "mouseeater.h"

MouseEater::MouseEater(std::function<bool (QMouseEvent *)> func, QObject *parent)
    : QObject(parent)
    , m_func(func)
{
    if (parent)
    {
        parent->installEventFilter(this);
    }
    else
        deleteLater();
}

bool MouseEater::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress
            || event->type() == QEvent::MouseMove
            || event->type() == QEvent::MouseButtonRelease
            || event->type() == QEvent::Wheel
            || event->type() == QEvent::MouseButtonDblClick)
    {
        if (m_func(static_cast<QMouseEvent *>(event)))
        {
            return true;
        }
    }
    return QObject::eventFilter(watched, event);
}
